package cn.wolfcode.web.controller;

import cn.wolfcode.qo.CustomerReportQuery;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 甘静斌 on 2020-07-24 21:15
 */

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    /**
     *
     * 表格形式的报表
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") CustomerReportQuery qo, Model model){

        model.addAttribute("pageResult", customerReportService.selectForList(qo));
        return "customerReport/list";
    }


    /**
     *
     * 柱状图(不用分页)
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/listBar")
    public String listBar(@ModelAttribute("qo") CustomerReportQuery qo, Model model){

        //查询数据库分组数据
        List<HashMap> list = customerReportService.listAll(qo);

        //先准备两个集合,分别存储x和y的数据
        List x = new ArrayList<>();
        List y = new ArrayList<>();

        //遍历sql查出来的集合,分别放入x和y集合中
        for (HashMap map : list) {

            x.add(map.get("groupType"));
            y.add(map.get("number"));
        }

        //共享到页面
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        model.addAttribute("x", JSON.toJSONString(x));
        model.addAttribute("y", JSON.toJSONString(y));
        return "customerReport/listBar";
    }


    /**
     * 饼状图
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("/listPie")
    public String listPie(@ModelAttribute("qo") CustomerReportQuery qo, Model model){

        List<HashMap> list = customerReportService.listAll(qo);

        //先准备两个集合,分别存储数据
        List legend = new ArrayList<>();
        List series = new ArrayList<>();

        for (HashMap map : list) {

            legend.add(map.get("groupType"));
            HashMap hashMap = new HashMap<>();
            hashMap.put("name", map.get("groupType"));
            hashMap.put("value", map.get("number"));
            series.add(hashMap);
        }
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        model.addAttribute("legend", JSON.toJSONString(legend));
        model.addAttribute("series", JSON.toJSONString(series));
        return "customerReport/listPie";
    }
}
